package com.zhoushihao.class0813;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Train {
    public int Rows = 20;
    public int Cols = 5;
    public boolean[] seats;
    public int[] seatsRestCount;

    public Train(){
        seats = new boolean[Rows*Cols + 1];
        seatsRestCount = new int[Rows + 1];
        Arrays.fill(seatsRestCount, Cols);
    }
    public List<Integer> assignSeats(int tickets){
        for (int i = 1; i <= Rows; i++) {
            if (seatsRestCount[i] >= tickets) {
                return assignConsecutiveSeats(i, tickets);
            }
        }
        return assignAnotheredSeats(tickets);
    }
    public List<Integer> assignConsecutiveSeats(int rows, int tickets){
        List<Integer> assignSeats = new ArrayList<>();
        int consecutiveSeats = 0;
        int start = (rows-1) * Cols + 1;
        int firstSeat = -1;
        for (int i = start; i < start + Cols; i++) {
            if(!seats[i]){
                consecutiveSeats++;
                if(consecutiveSeats == tickets){
                    firstSeat = i - tickets + 1;
                    break;
                }
            }else{
                consecutiveSeats = 0;
            }
        }
        for (int i = 0; i< tickets; i++){
            seats[firstSeat + i] = true;
            assignSeats.add(firstSeat + i);
        }
        seatsRestCount[rows]-=tickets;
        return assignSeats;
    }
    public List<Integer> assignAnotheredSeats(int tickets) {
        List<Integer> assignSeats = new ArrayList<>();
        int assignedCount = 0;
        for (int i = 1; i <= Rows && assignedCount < tickets; i++) {
            if (!seats[i]) {
                assignedCount++;
                assignSeats.add(i);
                seats[i] = true;
                int row = i / Cols + 1;
                seatsRestCount[row]--;
            }
        }
        return assignSeats;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] orders = new int[n];
        for (int i = 0; i < n; i++) {
            orders[i] = sc.nextInt();
        }
        Train train = new Train();
        for(int tickets:orders){
            List<Integer> assignSeats = train.assignSeats(tickets);
            for(int seats:assignSeats){
                System.out.print(seats + " ");
            }
            System.out.println();
        }
    }

}
